# -*- coding: utf-8 -*-
"""
-------------------------------------------------
   File Name：     string
   Description :
   Author :       Flyoung
   date：          2023/4/16
-------------------------------------------------
   Change Activity:
                   2023/4/16:
-------------------------------------------------
"""
from lib import epsilon, symbol_alias
from lib_utils import get_start_terminator, get_end_terminator


class String:
    def __init__(self, content: str):
        # 符号串
        self.content = content

    def __hash__(self):
        return hash(self.content)

    def __str__(self) -> str:
        return self.content

    def __eq__(self, other):
        if isinstance(other, str):
            return self.content == other
        if isinstance(other, String):
            return self.content == other.content
        return False

    def __add__(self, other):
        if self.content == epsilon:
            return String(other.content)
        if other.content == epsilon:
            return String(self.content)
        return String(self.content + other.content)

    def get_first_symbol(self):
        # 串的起始符号
        if len(self.content) > 1:
            if self.content[1] == "'":
                return self.content[:2]
            else:
                non_terminal = get_start_terminator(self.content)
                if non_terminal is not None:
                    return non_terminal
        if len(self.content) > 0:
            return self.content[0]
        return None

    def get_last_symbol(self):
        # 串的末尾符号
        if len(self.content) > 1:
            if self.content[-1] == symbol_alias:
                return self.content[-2:]
            else:
                non_terminal = get_end_terminator(self.content)
                if non_terminal is not None:
                    return non_terminal
        if len(self.content) > 0:
            return self.content[-1]
        return None

    def get_penultimate_symbol(self):
        # 获取倒数第二个符号
        content = self.content[:self.content.rfind(self.get_last_symbol())]
        if len(content) > 0:
            string = String(content)
            return string.get_last_symbol()
        return None

    def split_symbol(self):
        content = self.content
        pointer = 0
        symbols = list()
        while pointer < len(content):
            if content[pointer].isupper():
                # 形如：S'
                if pointer + 1 < len(content) and content[pointer + 1] == symbol_alias:
                    symbols.append(content[pointer:pointer + 2])
                    pointer += 2
                # 形如：S
                else:
                    symbols.append(content[pointer])
                    pointer += 1
            else:
                terminator = get_start_terminator(content[pointer:])
                # 形如：id
                if terminator is not None:
                    symbols.append(terminator)
                    pointer += len(terminator)
                # 形如：+
                else:
                    symbols += content[pointer]
                    pointer += 1
        return symbols
